﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// ColumnIndex,主要功能是为了提供一个转换机制，把名称转换成为列索引，
    /// 并且可以组合多个列，并为他们提供一个统一的访问方式。
    /// </summary>
    public sealed class ColumnIndex
    {
        private StringCollection m_IndexTable = new StringCollection();
        private StringCollection NameTable = new StringCollection();
        private int[] m_Index;

        /// <summary>
        /// 
        /// </summary>
        public ColumnIndex()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        private bool m_IsChange = false;


        /// <summary>
        /// 设定columnName
        /// </summary>
        /// <param name="args"></param>
        public void SetColumnName(params string[] args)
        {
            NameTable.AddRange(args);
            m_IsChange = true;
        }

        /// <summary>
        /// 设定ColumnIndex。
        /// </summary>
        /// <param name="indexes"></param>
        public void SetColumnIndex(params int[] indexes)
        {
            m_IndexTable.AddRange(ConvertExtent.ToArrayString(indexes));
            m_IsChange = true;
        }


        /// <summary>
        /// 获取索引号
        /// </summary>
        /// <param name="columnCollection"></param>
        /// <returns></returns>
        public int[] GetColumnIndexes(DataColumnCollection columnCollection)
        {
            if (m_Index == null || m_IsChange)
            {
                StringDictionary s = CreateMixColumns(columnCollection);
                m_Index = new int[s.Count];
                IEnumerator tors = s.Keys.GetEnumerator();
                int i = 0;
                while (tors.MoveNext())
                {
                    m_Index[i] = Convert.ToInt32(tors.Current);
                    i++;
                }
            }
            return m_Index;
        }

        /// <summary>
        /// 获取列组合数值,如果有多个列,那么列数值之间用";"号隔开
        /// </summary>
        /// <param name="dataRow">行</param>
        /// <returns>数值</returns>
        public string GetValues(DataRow dataRow)
        {
            int[] index = GetColumnIndexes(dataRow.Table.Columns);
            string result = "";
            for (int i = 0; i < index.Length; i++)
            {
                result += dataRow[index[i]] + ";";
            }
            return result;
        }

        private string m_Captions = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        public string GetCaptionNames(DataRow dataRow)
        {
            if (this.m_IsChange || m_Captions == null)
            {
                DataColumnCollection dcl = dataRow.Table.Columns;
                StringDictionary s = CreateMixColumns(dcl);
                m_Captions = null;
                IEnumerator tors = s.Values.GetEnumerator();
                bool first = true;
                while (tors.MoveNext())
                {
                    if (first)
                    {
                        m_Captions = "\"" + Convert.ToString(tors.Current) + "\"";
                        first = false;
                    }
                    else
                    {
                        m_Captions += ",\"" + Convert.ToString(tors.Current) + "\"";
                    }
                }
            }
            return m_Captions;
        }

        /// <summary>
        /// 创建一个Key为列索引 Value为列明成的集合
        /// </summary>
        /// <param name="columnCollection"></param>
        /// <returns></returns>
        private StringDictionary CreateMixColumns(DataColumnCollection columnCollection)
        {
            StringDictionary result = new StringDictionary();
            for (int i = 0; i < this.m_IndexTable.Count; i++)
            {
                int index = Convert.ToInt32(m_IndexTable[i]);
                result.Add(m_IndexTable[i], columnCollection[index].Caption);
            }

            for (int i = 0; i < this.NameTable.Count; i++)
            {
                string index = Convert.ToString(columnCollection.IndexOf(this.NameTable[i]));

                if (!result.ContainsKey(index))
                {
                    result.Add(index, NameTable[i]);
                }
            }
            return result;
        }

    }
}
